package in.co.codedoc.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

import com.opensymphony.xwork2.util.ValueStack;

public class JSONForEachTag 
	extends ComponentTagSupport
{
	@Override
	public Component getBean(ValueStack stack, HttpServletRequest req,
			HttpServletResponse res)
	{
		return new JSONForEach(stack);
	}
	
	@Override
	protected void populateParams()
	{
		((JSONForEach)getComponent()).setContainer(getContainer());
		((JSONForEach)getComponent()).setItem(getItem());
		super.populateParams();
	}
	
	public String getContainer()
	{
		return container;
	}

	public void setContainer(String container)
	{
		this.container = container;
	}
	
	private String container;

	public String getItem()
	{
		return item;
	}

	public void setItem(String item)
	{
		this.item = item;
	}
	
	@Override
	public int doEndTag() throws JspException
	{
		component = null;
		return EVAL_PAGE;
	}

	@Override
	public int doAfterBody() throws JspException
	{
		boolean again = component.end(pageContext.getOut(), getBody());

		if (again)
		{
			return EVAL_BODY_AGAIN;
		}
		else
		{
			if (bodyContent != null)
			{
				try
				{
					bodyContent.writeOut(bodyContent.getEnclosingWriter());
				}
				catch (Exception e)
				{
					throw new JspException(e.getMessage());
				}
			}
			return SKIP_BODY;
		}
	}

	private String item;
	private static final long serialVersionUID = 78548362756267L;	
}

